<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 19-2-27
 * Time: 下午4:31
 */

namespace common\models;

use common\components\e_divide\EDivide;

/**
 * Class EdiviCreditOrder
 * @package common\models
 *
 * @property EdiviCommonBackLogs[] $ediviCommonBackLogs
 * @property EdiviCreditOrderUsers[] $ediviCreditOrderUsers
 * @property EdiviOrderCard $ediviOrderCard
 * @property EdiviOrderDivi $ediviOrderDivi
 * @property EdiviOrderSendLogs[] $ediviOrderSendLogs
 *
 * @property EdiviOrderSendLogs $lastCreditSendLog
 * @property EdiviCommonBackLogs $resumeBackLog
 */
class EdiviCreditOrder extends \common\models\base\EdiviCreditOrder
{
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEdiviCommonBackLogs()
    {
        return $this->hasMany(EdiviCommonBackLogs::className(), ['edivi_credit_order_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEdiviCreditOrderUsers()
    {
        return $this->hasMany(EdiviCreditOrderUsers::className(), ['edivi_credit_order_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEdiviOrderCard()
    {
        return $this->hasOne(EdiviOrderCard::className(), ['edivi_credit_order_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEdiviOrderDivi()
    {
        return $this->hasOne(EdiviOrderDivi::className(), ['edivi_credit_order_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEdiviOrderSendLogs()
    {
        return $this->hasMany(EdiviOrderSendLogs::className(), ['edivi_credit_order_id' => 'id']);
    }

    /**
     * @return array|EdiviOrderSendLogs|null|\yii\db\ActiveRecord
     */
    public function getLastCreditSendLog()
    {
        $log = EdiviOrderSendLogs::find()->where(['edivi_credit_order_id' => $this->id, 'b_code' => EDivide::B_CODE_CREDIT])->orderBy(['created_at' => SORT_DESC])->one();
        return $log;
    }

    /**
     * @return array|EdiviCommonBackLogs|null|\yii\db\ActiveRecord
     */
    public function getResumeBackLog()
    {
        $log = EdiviCommonBackLogs::find()->where(['edivi_credit_order_id' => $this->id, 'trans_type' => EdiviCommonBackLogs::TRANS_TYPE_3])->orderBy(['created_at' => SORT_DESC])->one();
        return $log;
    }
}